package com.weetop.aklyscan.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.view.SimpleDraweeView;
import com.weetop.aklyscan.R;
import com.weetop.aklyscan.base.BaseUrl;
import com.weetop.aklyscan.bean.BannerInfo;

import java.util.ArrayList;

import static com.blankj.utilcode.util.SizeUtils.dp2px;

public class GoodImgAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private boolean isScrolling = false;
    private Context mContext;
    private ArrayList<BannerInfo> bannerInfos;


    public void setScrolling(boolean scrolling) {
        isScrolling = scrolling;
    }

    public GoodImgAdapter(Context context, ArrayList<BannerInfo> clothList) {
        this.mContext = context;
        this.bannerInfos = clothList;
        Fresco.initialize(mContext);

    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = View.inflate(mContext, R.layout.item_img_detal, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        final BannerInfo item = bannerInfos.get(position);
        ViewHolder holder = (ViewHolder) viewHolder;
        SimpleDraweeView iv_img = holder.iv_img;
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(dp2px(375),
                dp2px(375 * (item.getHeight() / (float) item.getWidth())));//两个400分别为添加图片的大小
        iv_img.setLayoutParams(params);
        if (!isScrolling) {
            iv_img.setImageURI(Uri.parse(BaseUrl.imgUrl + item.getPic()));
        } else {
            Glide.with(mContext).load(R.drawable.drawable_bg).into(iv_img);
        }
    }

    @Override
    public int getItemCount() {
        return bannerInfos.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        SimpleDraweeView iv_img;

        public ViewHolder(View view) {
            super(view);
            iv_img = (SimpleDraweeView) view.findViewById(R.id.iv_img);
        }
    }

}
